/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frm_JInternalFrameLapPhieuNhapSach.java
 *
 * Created on Aug 2, 2011, 6:10:28 PM
 */
package gui;


import dao.CTPhieuNhapDAO;
import dao.NhaXuatBanDAO;
import dao.PhieuNhapDAO;
import dao.SachDAO;
import dao.TheLoaiDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.CTPhieuNhapPOJO;
import pojo.NhaXuatBanPOJO;
import pojo.PhieuNhapPOJO;
import pojo.SachPOJO;
import pojo.TheLoaiPOJO;

/**
 *
 * @author HP
 */
public class frm_JInternalFrameLapPhieuNhapSach extends javax.swing.JInternalFrame {

    ArrayList<NhaXuatBanPOJO> dsnxb = new ArrayList<NhaXuatBanPOJO>();
    ArrayList<TheLoaiPOJO> dstheloai = new ArrayList<TheLoaiPOJO>();
    TheLoaiDAO theloaidao = new TheLoaiDAO();
    NhaXuatBanDAO nhaxbdao = new NhaXuatBanDAO();
    SachDAO sachdao = new SachDAO();
    ArrayList<SachPOJO> dssach = new ArrayList<SachPOJO>();
    DefaultTableModel dftablemodelSach = new DefaultTableModel();
    DefaultTableModel dftablemodelCTphieunhap = new DefaultTableModel();
    PhieuNhapDAO phieunhapdao = new PhieuNhapDAO();
    CTPhieuNhapDAO ctpndao = new CTPhieuNhapDAO();
    int stt = 1;
    int sttctpn = 1;
    /**
     *maphieunhap=-1 la chua them vao bang phieu nhap
     * khi maphieunhap>0 da them vao bang pheiu nhap luc nay
     * khi them vao bang chi tiet phieu nhap se lay ma phieu nhap nay
     * mac dinh khi mo form len thi se tao mot phieu nhap moi
     */
    int maphieunhap = -1;

    private void InitColumTable() {
        dftablemodelSach.addColumn("STT");
        dftablemodelSach.addColumn("Ma Sach");
        dftablemodelSach.addColumn("Ten Sach");
        dftablemodelSach.addColumn("So Luong Ton");
        // dftablemodelSach.addColumn("So Luong Ton");
        this.jtbDSSachVuaThem.setModel(dftablemodelSach);

        dftablemodelCTphieunhap.addColumn("STT");
        dftablemodelCTphieunhap.addColumn("Mã Phiếu Nhập");
        dftablemodelCTphieunhap.addColumn("Má Sách");
        dftablemodelCTphieunhap.addColumn("Số Lượng Nhập");
        dftablemodelCTphieunhap.addColumn("Đơn Giá Nhập");
        this.jtbDSChiTietPhieuNhap.setModel(dftablemodelCTphieunhap);
    }

    /** Creates new form frm_JInternalFrameLapPhieuNhapSach */
    public frm_JInternalFrameLapPhieuNhapSach() {
        initComponents();

        InitColumTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDSChiTietPhieuNhap = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jtxtTenSach = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jcmbTheLoai = new javax.swing.JComboBox();
        jcmbxNhaXuatBan = new javax.swing.JComboBox();
        jtxtSoLuongNhập = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jtxtDonGiaNhap = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jtxtMaSach = new javax.swing.JTextField();
        jbtnThemSach = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jtxtDonGiaBan = new javax.swing.JTextField();
        jcmbDSSach = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jchexboxNhapSachMoi = new javax.swing.JCheckBox();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbDSSachVuaThem = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Lap Phieu Nhap Sach");
        addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                formPropertyChange(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 204, 204));

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel10.setFont(new java.awt.Font("Tahoma", 3, 24));
        jLabel10.setText("Lập Phiếu Nhập Sách");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(418, 418, 418)
                .addComponent(jLabel10)
                .addContainerGap(490, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel10)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2), "Danh sách chi tiếp phiếu nhập"));

        jtbDSChiTietPhieuNhap.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDSChiTietPhieuNhap);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2), "Thông Tin Phiếu Nhập"));

        jtxtTenSach.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jtxtTenSach.setSelectionColor(new java.awt.Color(0, 0, 0));

        jLabel4.setText("Tên Sách");

        jLabel5.setText("Thể Loại");

        jLabel6.setText("Nhà Xuất Bản");

        jcmbTheLoai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbTheLoaiActionPerformed(evt);
            }
        });

        jcmbxNhaXuatBan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbxNhaXuatBanActionPerformed(evt);
            }
        });
        jcmbxNhaXuatBan.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jcmbxNhaXuatBanFocusGained(evt);
            }
        });

        jtxtSoLuongNhập.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jtxtSoLuongNhập.setSelectionColor(new java.awt.Color(0, 0, 0));

        jLabel7.setText("Số Lượng Nhập");

        jLabel8.setText("Đơn Giá Nhập");

        jtxtDonGiaNhap.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jtxtDonGiaNhap.setSelectionColor(new java.awt.Color(0, 0, 0));

        jLabel9.setText("Mã Sách");

        jtxtMaSach.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jtxtMaSach.setSelectionColor(new java.awt.Color(0, 0, 0));
        jtxtMaSach.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtxtMaSachCaretUpdate(evt);
            }
        });

        jbtnThemSach.setText("Them");
        jbtnThemSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemSachActionPerformed(evt);
            }
        });

        jLabel3.setText("Đơn Giá Bán");

        jtxtDonGiaBan.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jtxtDonGiaBan.setSelectionColor(new java.awt.Color(0, 0, 0));

        jcmbDSSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbDSSachActionPerformed(evt);
            }
        });

        jLabel11.setText("Sách");

        jchexboxNhapSachMoi.setText("Nhập Sách Mới");
        jchexboxNhapSachMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchexboxNhapSachMoiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(63, 63, 63))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jbtnThemSach, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jtxtDonGiaBan, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtDonGiaNhap, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtSoLuongNhập, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcmbxNhaXuatBan, 0, 244, Short.MAX_VALUE))
                    .addComponent(jcmbTheLoai, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jcmbDSSach, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jtxtTenSach, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addComponent(jtxtMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jchexboxNhapSachMoi)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbxNhaXuatBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jcmbTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbDSSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jchexboxNhapSachMoi))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtxtSoLuongNhập, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtDonGiaNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jtxtDonGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtnThemSach, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(201, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2), "Thông Tin Sách Vừa Thêm"));

        jtbDSSachVuaThem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jtbDSSachVuaThem);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcmbTheLoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbTheLoaiActionPerformed
        // TODO add your handling code here:
        try {

            HienThiCBXDanhSachSach_Theo_MaTheLoai_MaNhaXuatBan();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jcmbTheLoaiActionPerformed

    private void Init() {
        try {
            DefaultComboBoxModel dfcbxTheLoai = new DefaultComboBoxModel();
            DefaultComboBoxModel dfcbxNhaXB = new DefaultComboBoxModel();

            dstheloai = theloaidao.layDanhSach_TheLoai();

            for (TheLoaiPOJO i : dstheloai) {
                Vector vt = new Vector();
                vt.add(i.getMaTheLoai());
                vt.add(i.getTenTheLoai());
                //them mot dong voao model
                dfcbxTheLoai.addElement(vt);
            }
            this.jcmbTheLoai.setModel(dfcbxTheLoai);

            //
            dsnxb = nhaxbdao.layDSNhaXuatBan();

            for (NhaXuatBanPOJO i : dsnxb) {
                Vector vt = new Vector();
                vt.add(i.getMaNhaXuatBan());
                vt.add(i.getTenNhaXuatBan());
                dfcbxNhaXB.addElement(vt);
            }
            this.jcmbxNhaXuatBan.setModel(dfcbxNhaXB);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void jbtnThemSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemSachActionPerformed
        // TODO add your handling code here:
        try {
            SachPOJO sachdto = new SachPOJO();
            String masach = this.jtxtMaSach.getText();
            boolean kimtrathemsach = false;
            float dongianhap = Float.parseFloat(this.jtxtDonGiaNhap.getText());
            CTPhieuNhapPOJO ctpndto = new CTPhieuNhapPOJO();



            int soluongnhap = Integer.parseInt(this.jtxtSoLuongNhập.getText());
            if (maphieunhap < 0) {
                PhieuNhapPOJO pndto = new PhieuNhapPOJO();
                Date ngaynhap = new Date();
                pndto.setNgayNhap(ngaynhap);
                maphieunhap = phieunhapdao.ThemPhieuNhap_sql(pndto);
            }
            if (maphieunhap > 0) {
                maphieunhap=phieunhapdao.LayMaxPhieuNhap();
                if (this.jchexboxNhapSachMoi.isSelected() == false)//them sach moi
                {
                    sachdto = sachdao.layDSSach_TheoMaSach(masach);
                    if (sachdto.getMaSach() != "")//sach co trong csdl cap nhat lai so luong ton cua sach
                    {
                        int soluongton = sachdto.getSoLuongTon() + soluongnhap;
                        sachdto.setSoLuongTon(soluongton);
                        if (sachdao.capNhatSach(sachdto) == true)//da cap nhat thanh cong
                        {
                            kimtrathemsach = true;
                        }
                    }
                } else//nhan vao them cuon sach moi
                {
                    sachdto = sachdao.layDSSach_TheoMaSach(masach);
                    if (sachdto.getMaSach() != "")//sach co trong csdl cap nhat lai so luong ton cua sach
                    {
                        JOptionPane.showMessageDialog(this, "Cuốn Sách này đã tồn tại trong cơ sở dữ liệu bạn chỉ /r/n nhập số lượng nhập ", "Thông báo", JOptionPane.ERROR_MESSAGE);
                    } else//cuoi sach nay chua ton tai trong csdl thuc hien them cuon sach vao csdl
                    {
                        Vector vtnxb = (Vector) this.jcmbxNhaXuatBan.getSelectedItem();
                        Vector vttheloai = (Vector) this.jcmbTheLoai.getSelectedItem();
                        int matheloai = (Integer) vttheloai.get(0);
                        String manbx = (String) vtnxb.get(0);
                        sachdto.setMaNhaXuatBan(manbx);
                        sachdto.setMaTheLoai(matheloai);
                        sachdto.setMaSach(masach);
                        sachdto.setTenSach(this.jtxtTenSach.getText());
                        sachdto.setSoLuongTon(soluongnhap);
                        sachdto.setGiaBan(Float.parseFloat(this.jtxtDonGiaBan.getText()));
                        if (sachdao.themSach(sachdto) == true) {
                            kimtrathemsach = true;
                        }
                    }
                }

                if (kimtrathemsach == true) {
                    ctpndto.setMaPhieuNhap(maphieunhap);
                    ctpndto.setMaSach(masach);
                    ctpndto.setSoLuongNhap(soluongnhap);
                    ctpndto.setDonGiaNhap(dongianhap);

                    if (ctpndao.ThemChiTietPhieuNhap_sql(ctpndto) > 0)//them ct phieu nhap thanh cong
                    {

                        // if(sachdao.themSach(sachdto)==true) {
                        Vector vt = new Vector();
                        vt.add(stt++);
                        vt.add(sachdto.getMaSach());
                        vt.add(sachdto.getTenSach());
                        vt.add(sachdto.getSoLuongTon());
                        vt.add(sachdto.getGiaBan());
                        this.dftablemodelSach.addRow(vt);
                        // }

                        Vector vtctpn = new Vector();
                        vtctpn.add(sttctpn++);
                        vtctpn.add(maphieunhap);
                        vtctpn.add(masach);
                        vtctpn.add(ctpndto.getSoLuongNhap());
                        vtctpn.add(ctpndto.getDonGiaNhap());
                        this.dftablemodelCTphieunhap.addRow(vtctpn);

                    }
                   
                } else {
                    JOptionPane.showMessageDialog(this, "Thêm không thành công", "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
                }
            }
            else
            {
                 JOptionPane.showMessageDialog(this, "Thêm không thành công", "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
            }



        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jbtnThemSachActionPerformed

    private void jcmbDSSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbDSSachActionPerformed
        // TODO add your handling code here:
        try {
            Vector vtsach = (Vector) this.jcmbDSSach.getSelectedItem();
            String masach = (String) vtsach.get(0);

            SachPOJO sachdto = new SachPOJO();

            if (this.jchexboxNhapSachMoi.isSelected() == false) {
                sachdto = sachdao.layDSSach_TheoMaSach(masach);
                this.jtxtTenSach.setText(sachdto.getTenSach());
                this.jtxtMaSach.setText(sachdto.getMaSach());
                this.jtxtDonGiaBan.setText(String.valueOf(sachdto.getGiaBan()));

            } else {
                this.jtxtTenSach.setText("");
                this.jtxtMaSach.setText("");
                this.jtxtDonGiaBan.setText("");
            }

        } catch (Exception ex) {
        }

    }//GEN-LAST:event_jcmbDSSachActionPerformed

    private void formPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_formPropertyChange
        // TODO add your handling code here:
        Init();
        this.jtxtDonGiaBan.enable(false);
        this.jtxtMaSach.enable(false);
        this.jtxtTenSach.enable(false);
    }//GEN-LAST:event_formPropertyChange

    private void jcmbxNhaXuatBanFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jcmbxNhaXuatBanFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jcmbxNhaXuatBanFocusGained

    private void jcmbxNhaXuatBanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbxNhaXuatBanActionPerformed
        // TODO add your handling code here:
        try {

            HienThiCBXDanhSachSach_Theo_MaTheLoai_MaNhaXuatBan();
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_jcmbxNhaXuatBanActionPerformed

    private void jtxtMaSachCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtxtMaSachCaretUpdate
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtMaSachCaretUpdate

    private void jchexboxNhapSachMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchexboxNhapSachMoiActionPerformed
        // TODO add your handling code here:
        try {
            if (this.jchexboxNhapSachMoi.isSelected() == true) {
                this.jtxtDonGiaBan.enable(true);
                this.jtxtMaSach.enable(true);
                this.jtxtTenSach.enable(true);

                this.jtxtTenSach.setText("");
                this.jtxtMaSach.setText("");
                this.jtxtDonGiaBan.setText("");

            } else {
                this.jtxtDonGiaBan.enable(false);
                this.jtxtMaSach.enable(false);
                this.jtxtTenSach.enable(false);
            }
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_jchexboxNhapSachMoiActionPerformed
    private void HienThiCBXDanhSachSach_Theo_MaTheLoai_MaNhaXuatBan() {
        try {

            this.jcmbDSSach.removeAllItems();

            ArrayList<SachPOJO> dss = new ArrayList<SachPOJO>();
            Vector vtnxb = (Vector) this.jcmbxNhaXuatBan.getSelectedItem();
            Vector vttheloai = (Vector) this.jcmbTheLoai.getSelectedItem();
            int matheloai = (Integer) vttheloai.get(0);
            String manbx = (String) vtnxb.get(0);



            dss = sachdao.LaySach_Theo_MaTheLoai_MaNhaXuatBan(matheloai, manbx);
            DefaultComboBoxModel dfsach = new DefaultComboBoxModel();

            for (SachPOJO i : dss) {
                Vector v = new Vector();
                v.add(i.getMaSach());
                v.add(i.getTenSach());
                dfsach.addElement(v);
            }
            this.jcmbDSSach.setModel(dfsach);

            //
            //  this.jtxtDonGiaBan.enable(false);


        } catch (Exception ex) {
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnThemSach;
    private javax.swing.JCheckBox jchexboxNhapSachMoi;
    private javax.swing.JComboBox jcmbDSSach;
    private javax.swing.JComboBox jcmbTheLoai;
    private javax.swing.JComboBox jcmbxNhaXuatBan;
    private javax.swing.JTable jtbDSChiTietPhieuNhap;
    private javax.swing.JTable jtbDSSachVuaThem;
    private javax.swing.JTextField jtxtDonGiaBan;
    private javax.swing.JTextField jtxtDonGiaNhap;
    private javax.swing.JTextField jtxtMaSach;
    private javax.swing.JTextField jtxtSoLuongNhập;
    private javax.swing.JTextField jtxtTenSach;
    // End of variables declaration//GEN-END:variables
}
